<?php
// +----------------------------------------------------------------------------
// | Tke团队版权所属 [三十年河东三十年河西,莫欺少年穷.!]
// +----------------------------------------------------------------------------
// | Copyright (c) 2014 http://www.itdaodan.com All rights reserved.
// +----------------------------------------------------------------------------
// | Author: PHP@妖孽 <dino_ma@163.com>
// +----------------------------------------------------------------------------
/**
 +------------------------------------------------------------------------------
 * 客服系统-统计
 +------------------------------------------------------------------------------
 * @version   1.0
 * @since 2014-8-13
 +------------------------------------------------------------------------------
 */
namespace Admin\Controller;

class ServiceStatisticsController extends AdminController {

    /**
     * @todo    获取活动的销售网点（HTML）[公用]
     */
    public function getPointList(){
        
        $activity_id = (int)I('activity_id');
        if($activity_id == 0){
            $this->error('暂无数据');
        }
        $sale_list = D('ServiceApi')->getActivitySalePoint($activity_id);
        $result = '';
        if($sale_list){
            $result .= '<h4 style="margin:20px 0;">销售网点：</h4>';
            $count = count($sale_list);
            for($i=0;$i<$count;$i++){
                $result .= '<label class="fl"><input type="radio" name="sale_point_id" value="'.$sale_list[$i]['sale_point_id'].'" /> '.$sale_list[$i]['sale_point_name'].'　　</label>' ;
            }
        }
        $this->success($result);
    }
    
    
    /**
     * @todo    员工列表块（R）[公用]
     */
    public function getMemberList(){
        
        $type = (int)I('type', 0);
        $member_value = I('member_value', '');
        //员工列表
        $member_list = D('ServiceApi')->getDepartmentMember(1);
        $member_list = ($member_list)? $member_list : null ;
        if($member_value){
            $value_ids = explode(',', $member_value);
            foreach ($member_list as $key=>$val){
                foreach ($val['_child'] as $k=>$v){
                    $member_list[$key]['_child'][$k]['is_check'] = (in_array($v['member_id'], $value_ids))? 1 : 0 ;
                }
            }
        }
        $this->assign('index', 0);
        $this->assign('type', $type);
        $this->assign('base_list', $member_list);
        $this->display('Public:base_member');
    }
    
    /**
     * @todo    品类模块（R）[公用]
     */
    public function getCategoryList(){
    
        //品类列表
	    $category_list = D('ConfCategory')->getConfCategoryList(array('status' => 1));// 获取线下品类
        $this->assign('base_list', $category_list);
        $this->display('Public:base_category');
    }
    
    /**
     * @todo    需求列表块（R）[公用]
     */
    public function getDemandList(){

        $demand_value = I('demand_value', '');
        $is_value = 0;
        //需求列表
        $demand_list = D('ServiceApi')->getAllDemand();
        $demand_list = ($demand_list)? $demand_list : null ;
        if($demand_value){
            $is_value = 1;
            $value_ids = explode(',', $demand_value);
            foreach ($demand_list as $key=>$val){
                foreach ($val['_child'] as $k=>$v){
                    $demand_list[$key]['_child'][$k]['is_check'] = (in_array($v['demand_content_id'], $value_ids))? 1 : 0 ;
                }
            }
        }

        $this->assign('is_value', $is_value);
        $this->assign('base_list', $demand_list);
        $this->display('Public:base_demand');
    }
    
    /**
     * @todo    房屋需求块（R）[公用]
     */
    public function getHouseInfo(){
        
        //装修预算，装修时间，入户时间
        $this->display('Public:base_house');
    }
    
    /**
     * @todo    活动数据统计
     * @remark  活动数据统计（/ServiceStatistics/activity）
     */
    public function activity(){
        
        if(IS_POST){
            //获取统计数据
            $member_id = array_unique((array)I('member_id',''));
            $member_id = is_array($member_id) ? implode(',',$member_id) : $member_id;
            //活动统计数据
            $info = D('Statistics')->personalCount($member_id);
            $result = array();
            if($info){
                $result['status'] = 1;
                $result['info'] = $info;
            }else{
                $result['status'] = 0;
                $result['info'] = D('Statistics')->getError();
            }
            $this->ajaxReturn($result);
        }else{
            //活动列表
            $activity_list = D('ServiceApi')->getServiceActivity('1,2,3,4');
            $this->assign('activity_list', $activity_list);
            $this->meta_title = '活动数据统计';
            $this->display('');
        }
        
    }
    /**
     * @todo    销售网点转化（活动+人组合）
     */
    public function change(){

        if(IS_POST){
            $activity_id = (int)I('activity_id');
            $member_id = array_unique((array)I('member_id',0));
            $member_id = is_array($member_id) ? implode(',',$member_id) : $member_id;
            //个人转化统计
            $change_count = D('Statistics')->change($member_id, $activity_id);
            $result = array();
            if($change_count !== false){
                $result['status'] = 1;
                $result['info'] = $change_count;
            }else{
                $result['status'] = 0;
                $result['info'] = D('Statistics')->getError();
            }
            $this->ajaxReturn($result);
        }else{
            //活动列表
            $activity_list = D('ServiceApi')->getServiceActivity('1,2,3,4');
            $this->assign('activity_list', $activity_list);
            $this->meta_title = '销售网点转化';
            $this->display('');
        }
    }
    /**
     * @todo    销售网点报名
     * @param   activity_id     活动ID
     * @param   sale_point_id   网点ID
     * @param   start_time      开始时间
     * @param   end_time        结束时间
     */
    public function pointJoin(){
        
        if(IS_POST){
            //销售网点报名
            $info = D('Statistics')->pointJoin();
            $result = array();
            if($info){
                $result['status'] = 1;
                $result['info'] = $info;
            }else{
                $result['status'] = 0;
                $result['info'] = D('Statistics')->getError();
            }
            $this->ajaxReturn($result);
        }else{
            //活动列表
            $activity_list = D('ServiceApi')->getServiceActivity('1,2,3,4');
            $this->assign('activity_list', $activity_list);
            $this->meta_title = '销售网点报名';
            $this->display('');
        }
    }
    
    /**
     * @todo    名单报名统计
     */
    public function listJoin(){
        
        if(IS_POST){
            //名单报名统计
            $obj_main = D('Statistics');
            $info = $obj_main->listJoin();
            $result = array();
            if($info){
                $result['status'] = 1;
                $result['info'] = $info;
            }else{
                $result['status'] = 0;
                $result['info'] = $obj_main->getError();
            }
            $this->ajaxReturn($result);
        }else{
            //活动列表
            $obj_api = D('ServiceApi');
            $activity_list = $obj_api->getServiceActivity('1,2,3,4');
            //名单列表
            $name_list = $obj_api->getServiceRollCallList();
            $this->assign('activity_list', $activity_list);
            $this->assign('name_list', $name_list);
            $this->meta_title = '名单报名统计';
            $this->display('');
        }
    }
    
    /**
     * @todo    员工绩效
     * @param   array   member_id   员工ID   
     * @remark  员工绩效（/ServiceStatistics/personalAchieve）
     * @author  fang
     */
    public function personalAchieve(){
        
        if(IS_POST){
            $activity_id = (int)I('activity_id');
            $member_id = array_unique((array)I('member_id',0));
            $member_id = is_array($member_id) ? implode(',',$member_id) : $member_id;
            if($activity_id == 0){
                $this->ajaxReturn(array('status' => 0, 'info' => '请选择活动'));
            }
            if(empty($member_id)){
                $this->ajaxReturn(array('status' => 0, 'info' => '请选择员工'));
            }
            //全员绩效
            $obj_main = D('Statistics');
            $info = ($member_id > 0 && $activity_id > 0)? $obj_main->fullAchieve($member_id, $activity_id) : null ;
            $result = array();
            if($info){
                $result['status'] = 1;
                $result['info'] = $info;
            }else{
                $result['status'] = 0;
                $result['info'] = $obj_main->getError();
            }
            $this->ajaxReturn($result);
        }else{
            //活动列表
            $activity_list = D('ServiceApi')->getServiceActivity('1,2,3,4');
            $this->assign('activity_list', $activity_list);
            $this->meta_title = '员工绩效';
            $this->display('');
        }
    }
    
    /**
     * @todo    全员绩效
     * @param   array   member_id   员工ID   
     * @remark  全员绩效（/ServiceStatistics/fullAchieve）
     * @author  fang
     */
    public function fullAchieve(){

        if(IS_POST){
            //全员绩效
            $obj_main = D('Statistics');
            $info = $obj_main->fullAchieve('', (int)I('activity_id'));
            $result = array();
            if($info){
                $result['status'] = 1;
                $result['info'] = $info;
            }else{
                $result['status'] = 0;
                $result['info'] = $obj_main->getError();
            }
            $this->ajaxReturn($result);
        }else{
            //活动列表
            $activity_list = D('ServiceApi')->getServiceActivity('1,2,3,4');
            $this->assign('activity_list', $activity_list);
            $this->meta_title = '全员绩效';
            $this->display('');
        }
    }
    
    /**
     * @todo    班车统计
     * @param   int    activity_id  活动ID
     * @author  fang
     */
    public function bus(){

        $activity_id = (int)I('activity_id');
        //活动列表
        $activity_list = D('ServiceApi')->getServiceActivity('1,2,3,4');
        $result = D('Statistics')->busStatistics($activity_id);
        if($result){
            $this->assign('_data', $result['data']);
            $this->assign('_time', $result['time']);
        }else{
            $this->assign('_data', null);
            $this->assign('_time', null);
        }
        
        $this->meta_title = '班车统计';
        $this->assign('activity_list', $activity_list);
        $this->display();
    }
    
    /**
     * @todo    客户需求统计
     */
    public function customer(){

        if(IS_POST){
            //客户需求统计
            $obj_main = D('Statistics');
            $count = $obj_main->demandCount();
            $result = array();
            if($count !== false){
                $result['status'] = 1;
                $result['info'] = $count;
            }else{
                $result['status'] = 0;
                $result['info'] = $obj_main->getError();
            }
            $this->ajaxReturn($result);
        }else{
            $this->meta_title = '客户需求统计';
            $this->display('');
        }
       

    }
    
    /**
     * @todo    通话统计
     * @param   array   member_id   员工ID   
     * @author  fang
     */
    public function call(){
        
        if(IS_POST){
            $member_id = array_unique((array)I('member_id',0));
            $member_id = is_array($member_id) ? implode(',',$member_id) : $member_id;
            if(empty($member_id)){
                $this->ajaxReturn(array('status' => 0, 'info' => '请选择员工'));
            }
            //通话统计
            $obj_main = D('Statistics');
            $info = ($member_id)? $obj_main->call($member_id) : null ;
            $result = array();
            if($info){
                $result['status'] = 1;
                $result['info'] = $info;
            }else{
                $result['status'] = 0;
                $result['info'] = $obj_main->getError();
            }
            $this->ajaxReturn($result);
        }else{
            $this->meta_title = '通话统计';
            $this->display('');
        }
        

        
    
    }
    
    //-------------个人--------------
    /**
     * @todo    个人综合数据
     * @param   UID   员工ID   
     * @author  fang
     */
    public function personalCount(){

        if(IS_POST){
            //个人转化统计
            $obj_main = D('Statistics');
            $info = $obj_main->personalCount(UID);
            $result = array();
            if($info){
                $result['status'] = 1;
                $result['info'] = $info;
            }else{
                $result['status'] = 0;
                $result['info'] = $obj_main->getError();
            }
            $this->ajaxReturn($result);
        }else{
            //活动列表
            $activity_list = D('ServiceApi')->getServiceActivity('1,2,3,4');
            $this->assign('activity_list', $activity_list);
            $this->meta_title = '个人综合数据';
            $this->display('');
        }
    }
    
    /**
     * @todo    个人转化统计
     * @param   UID   员工ID   
     * @author  fang
     */
    public function personalChange(){

        if(IS_POST){
            //个人转化统计
            $obj_main = D('Statistics');
            $change_count = $obj_main->change(UID, (int)I('activity_id', 0));
            $result = array();
            if($change_count !== false){
                $result['status'] = 1;
                $result['info'] = $change_count;
            }else{
                $result['status'] = 0;
                $result['info'] = $obj_main->getError();
            }
            $this->ajaxReturn($result);
        }else{
            //活动列表
            $activity_list = D('ServiceApi')->getServiceActivity('1,2,3,4');
            $this->assign('activity_list', $activity_list);
            $this->meta_title = '个人转化统计';
            $this->display('');
        }
    }
    
    /**
     * @todo    个人通话统计
     * @param   UID   员工ID   
     */
    public function personalCall(){

        if(IS_POST){
            //通话统计
            $obj_main = D('Statistics');
            $info = $obj_main->call(UID);
            $result = array();
            if($info){
                $result['status'] = 1;
                $result['info'] = $info;
            }else{
                $result['status'] = 0;
                $result['info'] = $obj_main->getError();
            }
            $this->ajaxReturn($result);
        }else{
            //活动列表
            $activity_list = D('ServiceApi')->getServiceActivity('1,2,3,4');
            $this->assign('activity_list', $activity_list);
            $this->meta_title = '个人通话统计';
            $this->display('');
        }
    }

    /**
     * @todo    订单统计
     * @param   int activity_id 活动ID
     */
    public function orderStatistics(){

        $activity_id = (int)I('activity_id');
        
        $obj_main = D('Statistics');
        $info = $obj_main->orderStatistics($activity_id);
        if($info){
            $result['status'] = 1;
            $result['info'] = $info;
        }else{
            $result['status'] = 0;
            $result['info'] = $obj_main->getError();
        }
        $this->ajaxReturn($result);
    }
}
